﻿using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.Windows.Printing;
using StudySL4BizApp.Web;

namespace StudySL4BizApp
{
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using StudySL4BizApp.Web.Services;

    /// <summary>
    /// Home page for the application.
    /// </summary>
    public partial class Home : Page
    {
        private NorthWindContext _context;
        private EntityQuery<Customer> _query;

        /// <summary>
        /// Creates a new <see cref="Home"/> instance.
        /// </summary>
        public Home()
        {
            InitializeComponent();

            this.Title = ApplicationStrings.HomePageTitle;

            //_query = _context.GetCustomersQuery();

            //dataGrid1.ItemsSource = _context.Customers;
            //_context.Load(_query);


        }

        /// <summary>
        /// Executes when the user navigates to this page.
        /// </summary>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void dds_LoadedData(object sender, LoadedDataEventArgs e)
        {
            if (e.Error != null)
            {

            }
        }

        private void button1_Click(object sender, System.Windows.RoutedEventArgs e)
        {

        }

        private void orderDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
            else
            {
                var context = orderDomainDataSource.DomainContext as NorthWindContext;
                var names = (from Web.Customer c in context.Customers select c.ContactName).Distinct();
                customerFilter.ItemsSource = names;

            }

        }

        private void order_DetailsDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }



        }

        private void dataPager1_PageIndexChanged(object sender, System.EventArgs e)
        {


        }

        private void button1_Click_1(object sender, System.Windows.RoutedEventArgs e)
        {

        }

        private void button1_Click_2(object sender, System.Windows.RoutedEventArgs e)
        {

        }

        private void button1_Click_3(object sender, System.Windows.RoutedEventArgs e)
        {

        }

        private void printClick(object sender, System.Windows.RoutedEventArgs e)
        {
        }

        private void addButton_Click(object sender, System.Windows.RoutedEventArgs e)
        {


        }

        void form_Closed(object sender, System.EventArgs e)
        {
        }

        private void Remove_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        }

        private void shipperDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e)
        {

            if (e.HasError)
            {
                System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK);
                e.MarkErrorAsHandled();
            }
        }

        private void newOrder_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var order = orderDataGrid.SelectedItem as Web.Order;

            if (order != null)
            {
                var newOrderWindow = new Views.NewOrderDetail();
                newOrderWindow.DomainContext = orderDomainDataSource.DomainContext as NorthWindContext;
                newOrderWindow.SelectedOrder = order;

                newOrderWindow.Closed += new System.EventHandler(newOrderWindow_Closed);
                newOrderWindow.Show();

            }
        }

        void newOrderWindow_Closed(object sender, System.EventArgs e)
        {
            var newOrderWnd = sender as Views.NewOrderDetail;
            if (newOrderWnd.DialogResult == false)
            {
                return;
            }


            var order = orderDataGrid.SelectedItem as Web.Order;
            newOrderWnd.OrderDetail.Order = order;

            var context = orderDomainDataSource.DomainContext as Web.Services.NorthWindContext;
            context.Order_Details.Add(newOrderWnd.OrderDetail);

        }

        private void submit_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            var context = orderDomainDataSource.DomainContext as Web.Services.NorthWindContext;
            context.SubmitChanges();
        }

    }
}